import {isMobile} from './browser'

const dom = {
  /**
   * Parse second to time string
   *
   * @param {Number} second
   * @return {String} 00:00 or 00:00:00
   */
  secondToTime: second => {
    const add0 = num => (num < 10 ? '0' + num : '' + num)
    const hour = Math.floor(second / 3600)
    const min = Math.floor((second - hour * 3600) / 60)
    const sec = Math.floor(second - hour * 3600 - min * 60)
    return (hour > 0 ? [hour, min, sec] : [min, sec]).map(add0).join(':')
  },

  cumulativeOffset: element => {
    let top = 0

    let left = 0
    do {
      top += element.offsetTop || 0
      left += element.offsetLeft || 0
      element = element.offsetParent
    } while (element)

    return {
      top: top,
      left: left
    }
  },
  getElementViewLeft: element => {
    let actualLeft = element.offsetLeft
    let current = element.offsetParent
    const elementScrollLeft =
      document.body.scrollLeft + document.documentElement.scrollLeft
    if (
      !document.fullscreenElement &&
      !document.mozFullScreenElement &&
      !document.webkitFullscreenElement
    ) {
      while (current !== null) {
        actualLeft += current.offsetLeft
        current = current.offsetParent
      }
    } else {
      while (current !== null && current !== element) {
        actualLeft += current.offsetLeft
        current = current.offsetParent
      }
    }
    return actualLeft - elementScrollLeft
  },

  nameMap: {
    dragStart: isMobile() ? 'touchstart' : 'mousedown',
    dragMove: isMobile() ? 'touchmove' : 'mousemove',
    dragEnd: isMobile() ? 'touchend' : 'mouseup'
  }
}

const raf = (() =>
  window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  window.oRequestAnimationFrame ||
  window.msRequestAnimationFrame ||
  function (callback) {
    return window.setTimeout(callback, 1000 / 60)
  })()

const caf = (() =>
  window.cancelAnimationFrame ||
  window.webkitCancelAnimationFrame ||
  window.mozCanelAnimationFrame ||
  window.oCancelAnimationFrame ||
  window.msCancelAnimationFrame ||
  function (timer) {
    window.clearTimeout(timer)
  })()

const getAbsoluteUrl = url => {
  var a = document.createElement('a')
  a.setAttribute('href', url)
  return a.href
}

const getFileExt = url => {
  const fileExtReg = /^https?:.+(\.(\w+))\??(?![.\w/])/
  const res = url.match(fileExtReg)
  if (res) {
    return res[2]
  }
  return ''
}

const getMinetype = type => {
  var mineTypes = {
    aac: 'audio/mp4',
    mp4: 'video/mp4',
    f4v: 'video/mp4',
    m4v: 'video/mp4',
    mov: 'video/mp4',
    // 'm4a': 'audio/x-m4a', // converted to aac by source.js
    mp3: 'audio/mpeg',
    mpeg: 'audio/mpeg',
    ogv: 'video/ogg',
    ogg: 'video/ogg',
    oga: 'video/ogg',
    vorbis: 'video/ogg',
    webm: 'video/webm',

    // The following are not expected to work in Chrome
    f4a: 'video/aac',
    m3u8: 'application/vnd.apple.mpegurl',
    m3u: 'application/vnd.apple.mpegurl',
    hls: 'application/vnd.apple.mpegurl',
    flv: 'video/x-flv',
    dash: 'application/dash+xml',
    mpd: 'application/dash+xml'
  }
  return mineTypes[type] || ''
}

export {caf, raf, getAbsoluteUrl, getFileExt, getMinetype}

export default dom
